package com.junzhiit.nengji.app.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.app.service.GoodsImagesService;
import com.junzhiit.nengji.app.web.exception.GoodsImagesNotFound;
import com.junzhiit.nengji.app.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.app.web.vo.GoodsImagesAddVO;
import com.junzhiit.nengji.app.web.vo.GoodsImagesVO;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.dao.GoodsImagesDAO;
import com.junzhiit.nengji.model.GoodsImages;


@Service
public class GoodsImagesServiceImpl extends BaseServiceImpl implements GoodsImagesService {

	@Autowired
	private GoodsImagesDAO goodsImagesDAO;

	@Value("${upload.temp}")
	private String uploadPath;

	@Value("${upload.mall.goods}")
	private String goodsPath;
	
	@Value("${images.goods.source.path}")
	private String imagesSourcePath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@Override
	public ResultVO findByGoodsId(String goodsId) {
		ResultVO result = new ResultVO();

		List<GoodsImages> goodsImages = goodsImagesDAO.findByGoodsId(goodsId);
		if (goodsImages == null || goodsImages.isEmpty()) {
			return result;
		}

		Map<String, List<GoodsImagesVO>> map = new HashMap<String, List<GoodsImagesVO>>();
		for (GoodsImages goodsImage : goodsImages) {

			GoodsImagesVO goodsImagesVO = new GoodsImagesVO();
			BeanUtils.copyProperties(goodsImage, goodsImagesVO);
			String type = goodsImagesVO.getType().toString();

			if (map.containsKey(type)) {
				map.get(type).add(goodsImagesVO);
			} else {
				List<GoodsImagesVO> list = new ArrayList<GoodsImagesVO>();
				list.add(goodsImagesVO);
				map.put(type, list);
			}

		}
		result.setData(map);

		return result;
	}

	@Override
	public ResultVO add(List<GoodsImagesAddVO> goodsImagesAddVOs) {
		ResultVO result = new ResultVO();
		if (goodsImagesAddVOs == null || goodsImagesAddVOs.isEmpty()) {
			return result;
		}

		List<GoodsImages> GoodsImages = new ArrayList<GoodsImages>();
		for (GoodsImagesAddVO goodsImagesAddVO : goodsImagesAddVOs) {
			GoodsImages goodsImages = new GoodsImages();
			BeanUtils.copyProperties(goodsImagesAddVO, goodsImages);
			GoodsImages.add(goodsImages);
		}

		int data = goodsImagesDAO.batchInsert(GoodsImages);

		for (GoodsImagesAddVO goodsImagesAddVO : goodsImagesAddVOs) {
			//FileUtil.moveFile(uploadPath + goodsImagesAddVO.getFileName(), imagesSourcePath);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,goodsImagesAddVO.getFileName(), goodsPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+goodsImagesAddVO.getFileName());
			}
			//图片压缩
			//ThumbnailsUtil.toScale(imagesSourcePath+goodsImagesAddVO.getFileName(), goodsPath, goodsImagesAddVO.getFileName(), 1f, 0.5f);
		}

		result.setData(data);

		return result;
	}

	@Override
	public ResultVO del(String id) {
		ResultVO result = new ResultVO();
		GoodsImages goodsImages = goodsImagesDAO.query(id);
		if (goodsImages == null) {
			throw new GoodsImagesNotFound("图片不存在");
		}

		//FileUtil.removeFile(goodsPath + goodsImages.getFileName());
		//FileUtil.removeFile(imagesSourcePath + goodsImages.getFileName());
		AliyunOSSClientUtil.removeFile(backetName, goodsPath + goodsImages.getFileName());
		AliyunOSSClientUtil.removeFile(backetName, imagesSourcePath + goodsImages.getFileName());
		boolean data = goodsImagesDAO.delete(id);
		result.setData(data);
		return result;
	}
}
